<template>
  <div class="login-bg">
    <div class="parent " :style="{ height: scrollerHeight }">
      <div class="parent">
        <div class="xc-title">
          <div class="xc-tit">
            <div>
              <img class="login-head" src="@/assets/abScanCode1.png" alt="" />
              <p>
                <span class="title">宜铃停车手持端</span>
              </p>
            </div>
          </div>
        </div>

        <van-tabs line-width="120px" v-model="type">
          <!-- <van-tab title="验证码登录" name="msg">
            <el-input placeholder="手机号" v-model="phone" />
            <div class=" flex-betwen mt16">
              <el-input
                placeholder="请输入验证码"
                v-model="captcha"
                @change="actPwdChange"
                prefix-icon="el-icon-s-grid"
              />
              <img
                class="pwd-code"
                :src="captchatSrc"
                alt=""
                @click="changeCaptcha"
              />
            </div>

            <el-input
              v-if="captchaVerify"
              placeholder="请输入内容"
              v-model="smsCaptcha"
              class="mt16"
            >
              <template slot="append">
                <el-button
                  v-if="sendBtnFlag"
                  size="mini"
                  type="text"
                  @click.native="sendSmsCaptcha"
                  >获取验证码</el-button
                >
                <el-button v-else slot="right" type="warn" mini disabled>
                  <span>{{ showtime }}</span>
                </el-button>
              </template>
            </el-input>
          </van-tab> -->

          <van-tab title="账号登录" name="pswd">
            <el-input
              placeholder="账号"
              v-model="account"
              @change="actPwdChange"
              prefix-icon="el-icon-user"
            />
            <el-input
              placeholder="密码"
              v-model="password"
              @change="actPwdChange"
              prefix-icon="el-icon-lock"
              class="mt16"
              show-password
            />
            <!-- 添加验证码功能 -->
            <div v-if="captchaVerify">
              <div class=" flex-betwen mt16">
                <el-input
                  placeholder="请输入验证码"
                  v-model="captcha"
                  @change="actPwdChange"
                  prefix-icon="el-icon-s-grid"
                />
                <img
                  class="pwd-code"
                  :src="captchatSrc"
                  alt=""
                  @click="changeCaptcha"
                />
              </div>
            </div>
            <div class="xc-check mt10">
              <div>
                <input
                  ref="autoLogin"
                  type="checkbox"
                  v-model="checked"
                  @change="changee(checked)"
                /><span class="ml5">记住账号密码</span>
              </div>
              <router-link :to="{ name: 'forgetSecret' }"
                ><span>忘记密码</span></router-link
              >
            </div>
          </van-tab>
        </van-tabs>
      </div>
      <div class="xc-footer">
        <button class="myBtn" @click="btnClick">登录</button>
      </div>
    </div>
  </div>
</template>
<script>
import { XInput, Group, XButton, Toast } from "vux";

export default {
  name: "homeEdit",
  components: {
    XInput,
    Group,
    XButton,
    Toast
  },
  data() {
    return {
      type1: false, //用户判断 .parent 向上margin-top 多少的标记
      type2: false,
      account: "admin",
      password: "Cxkj@2024",
      captcha: "",
      rememberFlag: false,
      showCaptcha: false,
      captchatSrc: "",
      viewPwdFlag: false,
      type: "pswd",
      phone: "",
      smsCaptcha: "",
      sendBtnFlag: true,
      timeCounter: null,
      showtime: "60s后重新获取",
      checked: false,
      docmHeight: 0,
      key: "yllt0001",
      captchaVerify: false
    };
  },
  created: function() {
    this.isRemember();
    this.changeCaptcha();
  },

  methods: {
    // 记住密码
    isRemember() {
      this.$nextTick(() => {
        this.account = localStorage.getItem("account") || "";
        this.password = localStorage.getItem("password") || "";
        this.checked = localStorage.getItem("checked") || false;
      });
    },
    actPwdChange() {
      if (this.account && this.password) {
        this.changee(this.checked);
      }
    },
    changee(v) {
      if (v) {
        localStorage.setItem("account", this.account);
        localStorage.setItem("password", this.password);
        localStorage.setItem("checked", true);
      } else {
        localStorage.removeItem("account");
        localStorage.removeItem("password");
        localStorage.setItem("checked", false);
      }
    },
    sendSmsCaptcha() {
      if (!this.phone) {
        return this.$vux.toast.show({
          type: "warn",
          width: "60vw",
          text: '<p style="font-size:0.5rem">请填写账号！</p>'
        });
      }
    },
    countDownText(s) {
      this.showtime = `${s}s后重新获取`;
    },
    countDown(times) {
      const self = this;
      // 时间间隔 1秒
      const interval = 1000;
      let count = 0;
      self.timeCounter = setTimeout(countDownStart, interval);
      function countDownStart() {
        if (self.timeCounter == null) {
          return false;
        }
        count++;
        self.countDownText(times - count + 1);
        if (count > times) {
          clearTimeout(self.timeCounter);
          self.showtime = null;
          self.sendBtnFlag = true;
        } else {
          self.timeCounter = setTimeout(countDownStart, interval);
        }
      }
    },
    autoLogin() {
      this.rememberFlag = true;
    },
    // 切换验证码
    changeCaptcha() {
      this.baseService
        .get(`/user-center/sys/randomImage/${this.key}`)
        .then(data => {
          this.captchatSrc = data.result;
        });
    },
    pswdLogin() {},
    msgLogin() {},
    btnClick() {
      this.baseService
        .post("/user-center/sys/bizUser/login", {
          captcha: this.captcha,
          checkKey: this.key,
          password: this.password,
          username: this.account
        })
        .then(res => {
          if (res.code == 200) {
            this.abJwtSetting.setHeader(res.result.token);
            this.$store.commit("commonStore/SET_globalToken", res.result.token);
            this.$store.commit("commonStore/SET_userInfo", res.result.userInfo);
            localStorage.setItem(
              "userInfo",
              JSON.stringify(this.$store.state.commonStore.userInfo)
            );
            localStorage.setItem(
              "token",
              this.$store.state.commonStore.globalToken
            );
            this.$router.push({ path: "/xcHome" });
          } else {
            this.changeCaptcha()
            if (res.message == "验证码错误") {
              this.captchaVerify = true;
            }
            this.$vux.toast.show({
              type: "warn",
              width: "60vw",
              text: `<p style="font-size:0.5rem">${res.message}</p>`
            });
          }
        })
        .catch(err => {
          console.log("err---->", err);
        });

      // return;
    },
    setCookie(c_name, c_pwd, exdays) {
      window.localStorage.setItem("userName", c_name);
      window.localStorage.setItem("userPwd", c_pwd);
    },
    getCookie() {
      this.account = window.localStorage.getItem("userName");
      this.password = window.localStorage.getItem("userPwd");
      if (this.account && this.password) {
        this.checked = true;
        this.rememberFlag = true;
      } else {
        this.checked = false;
        this.rememberFlag = false;
      }
    },
    clearCookie() {
      this.setCookie("", "", -1);
    }
  },
  mounted() {
    this.getCookie();
    this.docmHeight = document.documentElement.clientHeight; //获取当前屏幕高度
  },

  watch: {
    globalFocusFlag(val) {
      if (!val) {
        this.type1 = false;
        this.type2 = false;
      }
    }
  },
  computed: {
    scrollerHeight: function() {
      // 110位一般手机键盘的输入高度
      return window.innerHeight - 75 + "px";
    },
    globalFocusFlag() {
      return this.$store.state.commonStore.globalFocusFlag;
    }
  }
};
</script>

<style scoped lang="less">
.login-bg {
  height: 100vh;
  position: relative;
  background-color: #054577;
  padding: 0px 20px;
  .parent {
    position: absolute;
    top: 0;
    left: 3%;
    width: 94%;
    transition: all 0.3s ease;

    &.type1 {
      top: -190px;
      transition: all 0.3s ease;
    }

    &.type2 {
      top: -250px;
      transition: all 0.3s ease;
    }

    .xc-title {
      // background-color: #FFF6F6FF;

      .xc-tit {
        height: 35vh;
        background-size: 100% 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        .login-head {
          width: 100px;
          height: 100px;
          border-radius: 50%;
        }

        img {
          width: 1.5rem;
          margin: auto;
          display: flex;
          justify-content: center;
          margin-bottom: 10px;
        }
      }

      p {
        display: flex;
        align-content: center;
        justify-content: center;
        color: #000000ff;
        font-size: 20px;

        span {
          display: flex;
          align-items: center;
          font-size: 20px;
        }
      }

      p.txt {
        font-size: 12px;
        font-weight: normal;
        color: #0e285d;
      }
    }

    .title {
      font-size: 0.6rem;
      text-align: center;
      color: #ffffff;
      font-weight: bold;
    }

    //start
    /deep/.van-tabs__content {
      // background-color: #fff;
      padding: 20px;
      border-radius: 8px 8px 8px 8px;
      background-color: #cee7ffa3;
    }

    /deep/.van-tabs__line {
      width: 0 !important;
      height: 0;
      background-color: transparent;
      border-left: 16px solid transparent;
      border-right: 16px solid transparent;
      // border-bottom: 10px solid #fff;
    }

    /deep/ .el-input__icon {
      line-height: 48px;
    }

    /deep/.van-tabs__nav {
      display: inline-block;
      width: 100%;
      margin: 20px 0px;
    }

    /deep/.van-tab {
      display: inline-block;
      margin-right: 30px;
    }

    /deep/.van-tab__text {
      font-size: 14px;
      color: #ffffff66;
    }

    /deep/.van-tab--active {
      .van-tab__text {
        font-size: 18px;
        color: rgb(255, 255, 255);
      }
    }

    /deep/button.weui-btn.weui-btn_plain-warn {
      border: 0;
      border-radius: 0;
      border-left: 1px solid #f0f0f0ff;
    }

    /deep/.weui-btn_plain-warn {
      color: #e60112ff !important;
    }

    /deep/.weui-cell:before {
      border: 0;
    }

    /deep/.weui-cell__ft {
      line-height: 1.23;
    }

    /deep/.el-input__inner {
      height: 48px;
      line-height: 48px;
      background-color: #f7f7f7ff;
      border: 0;
      font-size: 16px;
      color: #000000ff;
    }

    /deep/.el-input-group__append {
      border: 0;
      background-color: #f7f7f7ff;
      font-size: 14px;
      padding-left: 40px;

      button {
        margin-right: 0px;
      }

      span {
        font-size: 14px;
        color: #1659e2;
      }
    }

    /deep/.el-checkbox {
      color: #00000066;
    }

    .xc-check {
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: space-between;

      span {
        font-size: 14px;
        color: #00000066;
      }
    }

    .mt16 {
      margin-top: 16px;
    }

    .ml5 {
      margin-left: 5px;
    }

    //end
  }
}

.pwd {
  position: relative;

  .view-btn {
    position: absolute;
    right: 0.8rem;
    // right:1.5rem;
    padding: 0 0.2rem;
    top: 50%;
    transform: translate(0, -50%);
    z-index: 2;
    font-size: 0.7rem;
    line-height: 1;
    height: 100%;
    display: flex;
    align-items: center;
    color: #dc3019;
    padding-top: 1px;
  }
}
.pwd-code {
  border: 1px solid rgba(22, 21, 21, 0.61);
  width: 100px;
  height: 100%;
}

.xc-footer {
  position: absolute;
  bottom: 0.3rem;
  color: #333333;
  font-size: 0.3rem;
  margin: 0 auto;
  text-align: center;
  width: 100%;
  text-align: center;

  .myBtn {
    width: 80%;
    touch-action: none;
    border-radius: 20px;
    background-color: #1659e2;
    display: block;
    margin-left: auto;
    margin-right: auto;
    padding-left: 14px;
    padding-right: 14px;
    box-sizing: border-box;
    font-size: 18px;
    text-align: center;
    text-decoration: none;
    color: #ffffff;
    line-height: 2.33333333;
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
    overflow: hidden;
    border: none;
    // position: fixed;
    // left: 50%;
    // bottom: 2rem;
    // transform: translateX(-50%);
    box-shadow: 0 4px 12px rgba(22, 89, 226, 0.3);
    margin-bottom: 1rem;
  }
}

.icon {
  font-size: 0.7rem;
}

.van-tabs {
  position: relative;
  top: -1.5rem;

  /deep/ .van-tabs__nav {
    background: none;
  }
}
.flex-betwen {
  display: flex;
  justify-content: space-between;
  position: relative;
  align-items: center;
  background-color: white;
  padding-right: 5px;
}
</style>
